import { Component, OnInit } from '@angular/core'
import { MccService } from '@shared/components/mcc-select/mcc.service'
import { PostRiskService } from '../../post-risk.service'
import { PostRiskStoreService } from '../post-risk-store.service'
import { ActivatedRoute } from '@angular/router'
import { OperateType } from '@shared/interfaces/post-risk-control'

@Component({
  selector: 'app-tab-merchant',
  templateUrl: './tab-merchant.component.html',
  styles: [],
})
export class TabMerchantComponent implements OnInit {
  allChecked: Boolean
  indeterminate: Boolean
  mccOption: Array<{
    label: string;
    value: string;
    checked: Boolean;
  }> = []
  mccList: Array<{
    mccCode: string;
  }> = []
  type: number
  constructor(
    private mccService: MccService,
    private riskService: PostRiskService,
    private riskStore: PostRiskStoreService,
    public route: ActivatedRoute,
  ) {}

  ngOnInit() {
    this.allChecked = false
    this.indeterminate = false
    this.route.queryParams.subscribe(params => {
      this.type = Number(params.type)
    })
    if (this.type !== OperateType.copy) {
      // 将之前的mcc的选中
      const tmp = this.riskStore.value
      if (tmp.mcccList) {
        tmp.mcccList.forEach(item => {
          this.mccList.push({
            mccCode: item,
          })
        })
      } else {
        if (tmp.mcccInfoList) {
          tmp.mcccInfoList.forEach(item => {
            this.mccList.push({
              mccCode: item.mccCode,
            })
          })
        }
      }
    }
    this.riskService.getMccList().subscribe(data => {
      data.data.forEach(item => {
        const currentMcc = {
          label: item.mccCode + '' + item.mccEnName,
          value: item.mccCode,
          checked: false,
        }
        if (this.type !== OperateType.copy && this.mccList) {
          this.mccList.forEach(element => {
            if (element.mccCode === item.mccCode) {
              currentMcc.checked = true
            }
          })
        }
        this.mccOption.push(currentMcc)
      })
    })
    this.checkMcc()
  }

  checkValid(): boolean {
    this.pushDataToStore()
    return true
  }

  pushDataToStore(): void | boolean {
    // 将所有选中的数据放存起来
    const tmp = this.riskStore.value
    const mcccLists = []
    this.mccOption.forEach(item => {
      if (item.checked) {
        mcccLists.push(item.value)
      }
    })
    Object.assign(tmp, { mcccList: mcccLists })
    this.riskStore.setValue(tmp)
  }

  checkedAll() {
    this.indeterminate = false
    if (this.allChecked) {
      this.mccOption.forEach(item => {
        item.checked = true
      })
    } else {
      this.mccOption.forEach(item => {
        item.checked = false
      })
    }
  }

  checkMcc() {
    if (this.mccOption.every(item => item.checked === false)) {
      this.allChecked = false
      this.indeterminate = false
    } else if (this.mccOption.every(item => item.checked === true)) {
      this.allChecked = true
      this.indeterminate = false
    } else {
      this.indeterminate = true
    }
  }
}
